package com.breaktian.demoapp.ui.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.breaktian.assemble.threadpool.ThreadPool;
import com.breaktian.assemble.utils.ThreadUtil;
import com.breaktian.demoapp.R;
import com.breaktian.demoapp.provider.TestProvider;

/**
 * A simple {@link Fragment} subclass.
 */
public class ContactsFragment extends BaseFragment<TestProvider> {


    private View rootView;
    private TextView text;
    int num = 0;

    public ContactsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_contacts, container, false);
        initView();
        return rootView;
    }

    private void initView() {
        text = (TextView) rootView.findViewById(R.id.text);
        Button button = (Button) rootView.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ThreadUtil.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        startThread();
                    }
                }, 3000);

            }
        });

    }

    private void startThread() {
        ThreadPool.get().execute(new Runnable() {
            @Override
            public void run() {
                while(num<100){
                    try {
                        Thread.sleep(1000);

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    num++;
                    setText();
                }

            }
        });
    }

    private void setText() {
        ThreadUtil.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                text.setText(String.valueOf(num));
            }
        });
    }

    @Override
    protected TestProvider getProvider() {
        return null;
    }
}
